//
//  UserInformationViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/10/13.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "UserInformationViewController.h"
#import "GetDataMethods.h"
#import "Trips.h"
#import "TripsTableViewCel.h"
#import "BasicTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "UserInformationViewController.h"
#import "DownDetailsViewController.h"
#import "Model.h"
#import "Fit.h"
#import "MBProgressHUD.h"

@interface UserInformationViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *tripArray;
@property(nonatomic, retain)MBProgressHUD *progressView;

@end

@implementation UserInformationViewController
-(void)dealloc
{
    [_tableView release];
    [_tripArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = _name;
    [self changeColor];
}

-(void)changeColor{
    self.tableView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [Model shareInstance].BackgroundColor;
    [_tableView reloadData];
}

-(void)getData
{
    self.tripArray = [NSMutableArray array];
    [GetDataMethods getDataByString:[NSString stringWithFormat:@"http://api.breadtrip.com/v2/users/%@/", _string] BodyString:nil WithDataBlock:^(id data) {
        NSDictionary *dataDic = [data objectForKey:@"data"];
        NSArray *tripsArray = [dataDic objectForKey:@"trips"];
        for (NSDictionary *dic in tripsArray) {
            Trips *trip = [[Trips alloc]init];
            [trip setValuesForKeysWithDictionary:dic];
            [_tripArray addObject:trip];
        }
        [_tableView reloadData];
        [_progressView removeFromSuperview];
    }];
}
-(void)loadView
{
    [super loadView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"changeColor" object:nil];
    [self getData];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150 * H)];
    _tableView.tableHeaderView = headView;
    headView.backgroundColor = [Model shareInstance].BackgroundColor;
    [headView release];
    UIImageView *userImageView = [[UIImageView alloc]initWithFrame:CGRectMake(150 * W, 30 * H, 70 * W, 70 * W)];
    [userImageView sd_setImageWithURL:[NSURL URLWithString:_user] placeholderImage:[UIImage imageNamed:@"dengdai"]];
    userImageView.layer.cornerRadius = userImageView.frame.size.width / 2;
    userImageView.layer.masksToBounds = YES;
    [headView addSubview:userImageView];
    [userImageView release];
    
    //初始化加载中视图
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"玩命加载中";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.73 green:0.76 blue:0.77 alpha:.3];
    [self.view addSubview:_progressView];
    [_progressView release];
    
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    TripsTableViewCel *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[TripsTableViewCel alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_tripArray.count != 0) {
        cell.trip = _tripArray[indexPath.row];
    }
    cell.backgroundColor = [Model shareInstance].BackgroundColor;
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tripArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160 * H;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DownDetailsViewController *downVC = [[DownDetailsViewController alloc]init];
    if (_tripArray.count != 0) {
        downVC.string = (NSString *)[_tripArray[indexPath.row] Id];
    }
    [self.navigationController pushViewController:downVC animated:YES];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
